package com.emm.yixun.website.controller.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emm.yixun.common.model.BaseCodeValue;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.utils.VerifyIDNumber;
import com.emm.yixun.system.service.BaseCodeValueServiceFacade;

/**
 * 验证证件号码工具类
 * @author junze.liu
 *
 */
@Controller
@RequestMapping("/common/idNumber/")
public class VerifyIDNumberController {
	
	@Autowired
	private BaseCodeValueServiceFacade baseCodeValueServiceFacade;
	
	/**
	 * 验证身份证是否正确
	 * @param IDCard
	 * @return
	 */
	@RequestMapping("/verifyIDCard")
	@ResponseBody
	public boolean verifyIDCard(String code){
		return VerifyIDNumber.verifyIDCard(code);
	}

	/**
	 * 验证营业执照是否正确
	 * @param businessLicense
	 * @return
	 */
	@RequestMapping("/verifyBusinessLicense")
	@ResponseBody
	public boolean verifyBusinessLicense(String code){
		return VerifyIDNumber.verifyBusinessLicense(code);
	}
	
	/**
	 * 通过baseCodeId判断证件类型，并验证证件号
	 * @param baseCodeId
	 * @param code
	 * @return
	 */
	@RequestMapping("/verifyIDNumberByBaseCodeId")
	@ResponseBody
	public boolean verifyIDNumberByBaseCodeId(Long baseCodeId, String code) {
		Response<BaseCodeValue> res = baseCodeValueServiceFacade.findById(baseCodeId);
		if(!res.isSuccess() || res.getEntity() == null) {
			return false;
		}
		
		BaseCodeValue value = res.getEntity();
		if("身份证".equals(value.getCodeName())) {
			return VerifyIDNumber.verifyIDCard(code);
		}else if("营业执照".equals(value.getCodeName())) {
			return VerifyIDNumber.verifyBusinessLicense(code);
		}
		return true;
	}
}
